    //
//  HealthNewsCell.m
//  LoveHealth
//
//  Created by lanou3g on 14-9-18.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "HealthNewsCell.h"
#import "HealthNews.h"
#import "UIImageView+WebCache.h"

@interface HealthNewsCell ()
{
    UILabel * _titleLabel;  //资讯标题
    UILabel * _timeLabel;   //发布时间
    UILabel * _countLabel;  //浏览次数
    UIImageView *_imageView;//浏览
    UIImageView *_dateView;
}

//布局子视图
- (void)p_setupSubviews;
@end

@implementation HealthNewsCell
- (void)dealloc
{
    [_imageView release];
    [_timeLabel release];
    [_titleLabel release];
    [_countLabel release];
    [_dateView release];
    self.newsImageView = nil;
    self.news = nil;
    [super dealloc];
}
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code
        [self p_setupSubviews];
    }
    return self;
}

//布局子视图
- (void)p_setupSubviews
{
   
    //背景图片
    UIImageView * cellBackgroundView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, 100)];
    [self.contentView addSubview:cellBackgroundView];
    [cellBackgroundView release];
    
    //图片
    _newsImageView = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 90, 90)];
    _newsImageView.contentMode = UIViewContentModeScaleAspectFill;
    _newsImageView.clipsToBounds = YES;
    [cellBackgroundView addSubview:_newsImageView];

    
    //资讯标题
    _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(125, 15, 155, 50)];
    _titleLabel.font = [UIFont systemFontOfSize:13.0];
    _titleLabel.numberOfLines = 2;
    [cellBackgroundView addSubview:_titleLabel];

    
    
    //时间标题
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(145, 80, 90, 15)];
    _timeLabel.font = [UIFont systemFontOfSize:11.0];
    [cellBackgroundView addSubview:_timeLabel];
    
    _dateView = [[UIImageView alloc] initWithFrame:CGRectMake(125, 78, 16, 16)];
    _dateView.image = [UIImage imageNamed:@"date2.png"];
    [cellBackgroundView addSubview:_dateView];
    
    //浏览图标
    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(250, 78, 16, 16)];
    _imageView.image = [UIImage imageNamed:@"liulan_img"];
    [cellBackgroundView addSubview:_imageView];
    
    //浏览次数
    _countLabel = [[UILabel alloc] initWithFrame:CGRectMake(270, 80, 70, 15)];
    _countLabel.font = [UIFont systemFontOfSize:11.0];
    [cellBackgroundView addSubview:_countLabel];
    
    
}
//重写setter方法
- (void)setNews:(HealthNews *)news
{
    if (_news != news) {
        [_news release];
        _news = [news retain];
    }
    
    NSString * str = APIImageURL(news.img);
    //封装url地址
    NSURL * url = [NSURL URLWithString:str];
    //下载图片
    [_newsImageView sd_setImageWithURL:url placeholderImage:kPlaceholderImage];
    _titleLabel.text = news.title;
    
    //timeLabel显示时间
    //Sep 26, 2014 1:37:33 PM
    //MMM d, yyyy h:m:s a
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];

    NSLog(@"-----------------%@",news.time);
    [formatter setDateFormat:@"M d,yyyy h:m:s aaa"];

//    [formatter setDateFormat:@"MMM dd, yyyy hh:mm:ss a"];

    NSLocale * local =[[[NSLocale alloc] initWithLocaleIdentifier:@"en_US"]  autorelease];
    [formatter setLocale: local];
    
    
    NSDate * dateTime = [formatter dateFromString:news.time];

    NSLog(@"=================%@",dateTime);
   // [formatter setDateFormat:@"yyyy-M-d hh:mm:ss"];

    [formatter setDateFormat:@"yyyy-M-d HH:mm"];

    _timeLabel.text = [formatter stringFromDate:dateTime];
    [_timeLabel sizeToFit];
    [formatter release];
    
    _countLabel.text = [NSString stringWithFormat:@":%ld次",news.count];
    [_countLabel sizeToFit];
}

- (void)awakeFromNib
{
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
